package com.sleep.psy.external_server.business.main.controller;


import com.alibaba.fastjson.JSON;
import com.sleep.psy.external_server.business.evaluation.entity.dto.TrainingReport;
import com.sleep.psy.external_server.business.evaluation.service.EvaluationReportService;
import com.sleep.psy.external_server.business.main.entity.MainUser;
import com.sleep.psy.external_server.business.main.entity.vo.LoginReq;
import com.sleep.psy.external_server.business.main.entity.vo.RegistReq;
import com.sleep.psy.external_server.business.main.entity.vo.UpdateUserReq;
import com.sleep.psy.external_server.business.main.service.MainUserService;
import com.sleep.psy.external_server.result.Result;
import com.sleep.psy.external_server.result.ResultEnum;
import com.sleep.psy.external_server.result.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-12
 */
@Api(tags = "用户相关")
@RestController
@RequestMapping("/mainUser")
public class MainUserController {

    protected Logger logger = LoggerFactory.getLogger(MainUserController.class);

    @Autowired
    private MainUserService mainUserService;

    @Autowired
    private EvaluationReportService evaluationReportService;

    /**
     * 用户登陆
     * @author wenliguo
     * @date 2025/8/12 11:10
     * @param req 请求参数
     * @return com.sleep.psy.external_server.result.Result
    */
    @PostMapping("/login")
    @ApiOperation(value = "用户登陆")
    public Result<MainUser> login(@Valid @RequestBody LoginReq req) {

        logger.info("用户登陆参数:{}", JSON.toJSONString(req));
        try {
            return mainUserService.login(req);
        } catch (Exception e) {
            logger.error("用户登陆出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }


    /**
     * 更新用户信息
     * @author wenliguo
     * @date 2025/8/12 11:42
     * @param req 请求
     * @return com.sleep.psy.external_server.result.Result
    */
    @PostMapping("/updateUser")
    @ApiOperation(value = "更新用户信息")
    public Result<MainUser> updateUser(@Valid @RequestBody UpdateUserReq req) {

        logger.info("更新用户信息参数:{}", JSON.toJSONString(req));
        try {
            return mainUserService.updateUser(req);
        } catch (Exception e) {
            logger.error("更新用户信息出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }


    /**
     * 注册用户
     * @author wenliguo
     * @date 2025/8/12 14:23
     * @param req 请求
     * @return com.sleep.psy.external_server.result.Result<com.sleep.psy.external_server.main.entity.MainUser>
    */
    @PostMapping("/registUser")
    @ApiOperation(value = "注册用户")
    public Result<Long> registUser(@Valid @RequestBody RegistReq req) {

        logger.info("注册用户参数:{}", JSON.toJSONString(req));
        try {
            return mainUserService.registUser(req);
        } catch (Exception e) {
            logger.error("注册用户出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 通过用户id获取用户信息
     * @author wenliguo
     * @date 2025/8/12 14:28
     * @param userId 用户id
     * @return com.sleep.psy.external_server.result.Result<com.sleep.psy.external_server.main.entity.MainUser>
    */
    @GetMapping("/selectByUserId")
    @ApiOperation(value = "获取用户信息")
    public Result<MainUser> selectByUserId(@RequestParam(value = "userId") Long userId) {

        logger.info("获取用户信息参数userId:{}", userId);

        try {
            return mainUserService.selectByUserId(userId);
        } catch (Exception e) {
            logger.error("获取用户信息出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }


    /**
     * 通过用户id获取全部方案
     * @author wenliguo
     * @date 2025/8/18 09:59
     * @param userId 用户id
     * @return com.sleep.psy.external_server.result.Result<java.util.HashMap<java.lang.String,java.lang.Object>>
    */
    @GetMapping("/selectUserScheme")
    @ApiOperation(value = "通过用户id获取全部方案")
    public Result<HashMap<String, Object>> selectUserScheme(@RequestParam(value = "userId") Long userId) {

        logger.info("通过用户id获取全部方案参数userId:{}", userId);
        try {
            return mainUserService.selectUserScheme(userId);
        } catch (Exception e) {
            logger.error("通过用户id获取全部方案出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

    /**
     * 通过用户id查询训练报告
     * @author wenliguo
     * @date 2025/9/9 11:52
     * @param userId 用户id
     * @return com.sleep.psy.external_server.result.Result<java.util.List<com.sleep.psy.external_server.business.evaluation.entity.dto.TrainingReport>>
     */
    @GetMapping("/selectTrainingReport")
    @ApiOperation(value = "通过用户id查询训练报告")
    public Result<List<TrainingReport>> selectTrainingReport(@RequestParam(value = "userId") Long userId) {
        try {
            return ResultUtil.success(evaluationReportService.selectTrainingReport(userId));
        } catch (Exception e) {
            logger.error("通过用户id查询训练报告出错", e);
            return ResultUtil.error(ResultEnum.SERVER_INTERNAL_ERROR);
        }
    }

}

